<include file="Index/header"/>
<header class="am-g my-head">
  <div class="am-u-sm-12 am-article">
    <br>
    <h1 class="am-article-title">学生登录</h1>
    <br>
  </div>
</header>
<body>
    <div class="am-g">
        <div class="am-u-sm-6 am-u-md-8 am-u-sm-centered">
            <hr>
            <form class="am-form">
                <label>用户名</label>
                <input type="text" name="username" id="username">
                <br>
                <label>密码</label>
                <input type="password" name="password" id="password">
                <br>
            </form>
            <button name="login-btn" id="login-btn" class="am-btn am-btn-primary am-u-sm-centered">登录</button>
            <hr>
        </div>
  </div>
  <script type="text/javascript">
    $(document)
      .ready(function($) {
        $("#login-btn")
          .on('click', function(argument) {
            console.log("clicked");
            var username = $("#username").val();
            var password = $("#password").val();
            if (username == '' || password == '') {
              return;
            }
            $.ajax({
                url: '{:U("/Home/Student/checkLogin")}',
                type: 'POST',
                dataType: 'json',
                data: {
                  username: username,
                  password: password
                }
              })
              .done(function(response) {
                console.log("ajax success");
                if (response.status == 'success') {
                  window.location.href = '{:U("/Home/Student/index")}';
                } else {
                  alert(response.message);
                }
              })
              .fail(function() {
                console.log("error");
              })
              .always(function() {
                console.log("complete");
              });

          });

        function doCheck() {
          $.ajax({
            type: "POST", url: baseUrl + "/Home/User/reg?" + Math.random(),
            dataType: "json",
            data: {
              "name": $("#name").val(),
              "phone": $("#phone").val(),
              "checkcode": $("#checkcode").val(),
              "sex": $("input[name='sex']:checked").val()
            },
            success: function(data) {
              console.log(data);
              $.mobile
                .loading("hide");

              if (data.code == 1001) {
                // 信息不完整 不动
              } else if (data.code == 1002) {
                // 已注册 跳转主页
                $.mobile
                  .changePage(baseUrl + "/Home/Index/Index", {
                    reloadPage: true
                  });
              } else if (data.code == 1003) {
                // 注册成功 跳转主页
                $.mobile
                  .changePage(baseUrl + "/Home/Index/Index", {
                    reloadPage: true
                  });
              } else if (data.code == 1004) {
                // 更新失败 不动

              } else if (data.code == 1005) {
                // checkCode 错误

              }
              toast(data.message);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
              $.mobile
                .loading("hide");
              toast("提交错误，请重新提交");
              //console.log(request);

              console.log(XMLHttpRequest.status);
              console.log(XMLHttpRequest.readyState);
              console.log(textStatus);
            }
          });
        }
      });
  </script>
</body>
